// pages/service/authentication/index.js
var api = require("../../../../api.js"),
  QQMapWX = require("../../../../qqmap-wx.js"),
  app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userTypeArr: [{
      id: 1,
      name: '跑腿人员'
    },
    {
      id: 2,
      name: '家政人员'
    },
    {
      id: 3,
      name: '代驾人员'
    }
    ],
    userType: '',
    name: '',
    idcard: '',
    cardImg: '',
    cardImgf: '',
    carcardImg: '',
    carcardImgf:'',
    carcodes:''
  },
  userTypePickerChange: function (e) {
    let _index = e.detail.value;
    let currentItem = this.data.userTypeArr[_index];
    this.setData({
      userType: currentItem
    });
  },
  changeInputData: function (e) {
    var that = this;
    var inputName = e.currentTarget.dataset.name;
    var inputVlaue = e.detail.value;
    if (inputName == "name") {
      that.setData({
        name: inputVlaue
      });
    }
    if (inputName == "idcard") {
      that.setData({
        idcard: inputVlaue
      });
    }
    if (inputName == "carcodes") {
      that.setData({
        carcodes: inputVlaue
      });
    }
    
  },
  sendRequest: function () {
    var that = this;
    if (that.validate()) {
      // TODO 发送请求

      app.request({
        url: api.default.attestation,
        method: 'post',
        data: {
          usertype: that.data.userType.id,
          cid: wx.getStorageSync('cash').cid,
          uname: that.data.name,
          card: that.data.idcard,
          carcodes: that.data.carcodes,
          cardimg: that.data.cardImg, //身份证正面
          cardimgf: that.data.cardImgf,//身份证反面
          carcardimgs: that.data.carcardImg,//健康证，//驾驶证正面
          carcardImg:that.data.carcardImgf //驾驶证反面
        },
        success: function (e) {
          if (e.code == 1) {
            wx.setStorage({
              key: 'cash',
              data: {
                status: 2
              },
            })
            wx.showModal({
              title: '提示',
              showCancel: false,
              content: e.msg,
              success: function () {
                wx.redirectTo({
                  url: '../auth-success/index',
                })
              }
            })
          } else {
            wx.showToast({
              icon: 'none',
              mask: false,
              title: e.msg,
            })
          }
        }
      })

    }
  },
  validate: function () {
    var that = this;


    if (!that.data.name || that.data.name.length <= 0) {
      wx.showToast({
        title: '姓名不得为空',
        icon: 'none',
        mask: true,
      });
      return false;
    }
    if (!that.data.idcard || that.data.idcard.length != 15 && that.data.idcard.length != 18) {
      wx.showToast({
        title: '身份证号码不得为空',
        icon: 'none',
        mask: true,
      });
      return false;
    }
    if (!that.data.cardImg || that.data.cardImg == '') {
      wx.showToast({
        title: '请上传身份证照片',
        icon: 'none',
        mask: true,
      });
      return false;
    }
    return true;
  },
  chooseCard: function () {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        // TODO 执行图片上传
        wx.saveFile({
          tempFilePath: tempFilePaths[0],
          success: function (res) {
            var savedFilePath = res.savedFilePath;
            wx.uploadFile({
              url: api.default.uploadfile,
              filePath: savedFilePath,
              header: {

                'content-type': 'application/x-www-form-urlencoded'

              },
              name: 'image',
              success: function (e) {
                var res = JSON.parse(e.data);

                if (res.code == 1) {
                  that.setData({
                    cardImg: res.src
                  });
                } else {
                  wx.showModal({
                    title: '提示',
                    content: '图片上传失败',
                    showCancel: false,
                  })
                }
              }
            })


          }
        })
      }
    });
  },
  chooseCardf: function () {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        // TODO 执行图片上传
        wx.saveFile({
          tempFilePath: tempFilePaths[0],
          success: function (res) {
            var savedFilePath = res.savedFilePath;
            wx.uploadFile({
              url: api.default.uploadfile,
              filePath: savedFilePath,
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              name: 'image',
              success: function (e) {
                var res = JSON.parse(e.data);
                if (res.code == 1) {
                  that.setData({
                    cardImgf: res.src
                  });
                } else {
                  wx.showModal({
                    title: '提示',
                    content: '图片上传失败',
                    showCancel: false,
                  })
                }
              }
            })
          }
        })
      }
    });
  },
  chooseCarCard: function () {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        // TODO 执行图片上传
        wx.saveFile({
          tempFilePath: tempFilePaths[0],
          success: function (res) {
            var savedFilePath = res.savedFilePath;
            console.log(savedFilePath);
            wx.uploadFile({
              url: api.default.uploadfile,
              filePath: savedFilePath,
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              name: 'image',
              success: function (e) {
                var res = JSON.parse(e.data);
                if (res.code == 1) {
                  that.setData({
                    carcardImg: res.src
                  });
                } else {
                  wx.showModal({
                    title: '提示',
                    content: '图片上传失败',
                    showCancel: false,
                  })
                }
              }
            })
          }
        })
      }
    });
  },
  chooseCarCards:function() {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        // TODO 执行图片上传
        wx.saveFile({
          tempFilePath: tempFilePaths[0],
          success: function (res) {
            var savedFilePath = res.savedFilePath;
            console.log(savedFilePath);
            wx.uploadFile({
              url: api.default.uploadfile,
              filePath: savedFilePath,
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              name: 'image',
              success: function (e) {
                var res = JSON.parse(e.data);
                if (res.code == 1) {
                  that.setData({
                    carcardImgf: res.src
                  });
                } else {
                  wx.showModal({
                    title: '提示',
                    content: '图片上传失败',
                    showCancel: false,
                  })
                }
              }
            })
          }
        })
      }
    });
  }
})